//
//  ListViewController.m
//  Estate
//
//  Created by wangdongdong on 16/9/6.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "ListViewController.h"


@interface ListViewController ()<UIScrollViewDelegate>


// 滑动视图
@property (strong ,nonatomic) UIScrollView *contentScrollview;


@end


@implementation ListViewController


// 滑动视图
- (UIScrollView *)contentScrollview {
    if(_contentScrollview == nil){
        _contentScrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 118, DEF_SCREEN_WIDTH, DEF_SCREEN_HEIGHT)];
    }
    return _contentScrollview;
}

// 头部选择器
- (UISegmentedControl *)headerSegment {
    if(_headerSegment == nil){
        _headerSegment = [[UISegmentedControl alloc]initWithItems:self.headerSegmentArr];
        _headerSegment.frame = CGRectMake(10, 74, DEF_SCREEN_WIDTH - 20, 35 * BIZ);
    }
    return _headerSegment;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加滚动视图
    [self.view addSubview:self.contentScrollview];
    // 添加选择器
    [self.view addSubview:self.headerSegment];
    //创建视图样式
    [self createView];
}

#pragma mark -创建视图
- (void)createView {
    self.title = @"智慧监理";
    self.view.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
    [self.contentScrollview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerSegment.mas_bottom).offset(10);
        make.left.mas_offset(0);
        make.right.mas_offset(0);
        make.height.offset(667*BIZ);
    }];
    // 默认选中
    self.headerSegment.selectedSegmentIndex = 0;
    // 设置选择器样式
    [_headerSegment setTintColor:[TTTheme defaultManger].defaultColor];
    // 未选中颜色
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[TTTheme defaultManger].selectColor,NSFontAttributeName:[UIFont systemFontOfSize:16.*BIZ]} forState:UIControlStateNormal];
    // 选中颜色字体大小
    [_headerSegment setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:[UIFont systemFontOfSize:16.*BIZ]} forState:UIControlStateSelected];
    
    // 设置滚动视图
    [self setUpScrollView];
    // 设置子视图
    [self setUpChildViewControll];
    // 为选择器添加事件
    [_headerSegment addTarget:self action:@selector(segmentSelect:) forControlEvents:UIControlEventValueChanged];
}

// 设置滚动视图属性
- (void)setUpScrollView {
    _contentScrollview.pagingEnabled = YES;
    _contentScrollview.bounces = NO;
    _contentScrollview.delegate = self;
    _contentScrollview.showsHorizontalScrollIndicator = NO;
    // 方向锁
    _contentScrollview.directionalLockEnabled = YES;
    // 取消自动布局
    self.automaticallyAdjustsScrollViewInsets = NO;
    _contentScrollview.contentSize = CGSizeMake(375 * self.headerSegmentArr.count * BIZ, DEF_SCREEN_HEIGHT);
}

// 设置控制的每一个子控制器
-(void)setUpChildViewControll {
    for (int i = 0; i < self.VCArr.count; i++) {
        UIViewController *vController = self.VCArr[i];
        // 指定该控制器为其子控制器
        [self addChildViewController:vController];
        
        // 设置控制器的位置
        CGRect inspectionRect = vController.view.frame;
        inspectionRect.origin.x = DEF_SCREEN_WIDTH * i;
        inspectionRect.size.height = CGRectGetHeight(_contentScrollview.frame);
        vController.view.frame = inspectionRect;
        
        // 将视图view加入到scrollview上
        [self.contentScrollview addSubview:vController.view];
    }
}

// 点击选择器事件
- (void)segmentSelect:(UISegmentedControl*)seg {
    NSInteger index = seg.selectedSegmentIndex;
    CGRect frame = self.contentScrollview.frame;
    frame.origin.x = index *CGRectGetWidth(self.contentScrollview.frame);
    frame.origin.y = 0;
    [self.contentScrollview scrollRectToVisible:frame animated:YES];
}

# pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat offSetX = scrollView.contentOffset.x;
    NSInteger ratio = round(offSetX / DEF_SCREEN_WIDTH);
    _headerSegment.selectedSegmentIndex = ratio;
}


@end



